// pages/user/putForwardPrice/putForwardPrice.js
var app = getApp();
var isRepeat = false;
var withdrawConfig= [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prohibit: false, //选择银行卡弹窗
    isChoose: true, //未选择提现账户
    banks: [], //银行卡列表
    workers_info: [], //经销商账户信息
    bank_id: 0,
    total: 0,
    money: 0,
    poundage: 0,
    allMoney: '',
    actualMoney:'',//实际到账
    msg:'',
    isForward: false,//是否能提现
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.withdrawConfig();
    this.withdrawDepositInfo();
    isRepeat = false;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


  // 输入提现金额
  getMoney(e) {
    var that = this;
    //账户余额
    var account_balance = parseFloat(that.data.workers_info.available_money);
    //输入金额
    var val = e.detail.value;
    //判断最后一位是否为小数点
    var check = val.substr(val.length - 1,1);
    //验证金额
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if (!reg.test(val) && check != '.') {
      account_balance = parseFloat(account_balance).toFixed(2);
      app.showToast('请输入正确的提现金额');
      that.setData({
        allMoney: '',
        poundage: 0,
        money: 0,
        total: account_balance
      });
      return;
    } else if ((val.split('.')).length - 1 > 1){
      account_balance = parseFloat(account_balance).toFixed(2);
      app.showToast('请输入正确的提现金额');
      that.setData({
        allMoney: '',
        poundage: 0,
        money: 0,
        total: account_balance
      });
      return;
    } else if ((val.indexOf('.')) == 0) {
      that.setData({
        allMoney: '',
        msg: '请输入正确的提现金额',
        poundage: 0,
        money: 0,
        total: account_balance
      });
      return;
    }
    
    var percentage = parseFloat(withdrawConfig.value.percentage);
    var min = parseFloat(withdrawConfig.value.min);
    var max = parseFloat(withdrawConfig.value.max);
    var min_money = parseFloat(withdrawConfig.value.min_money);//最低提现金额
    var max_money = parseFloat(withdrawConfig.value.max_money);//最高提现金额

    // 判断是否能提现
    var isForward = '';
    if (val < min_money || val > max_money) {
      isForward = false;
    }else{
      isForward = true;
    }
    that.setData({
      isForward: isForward
    })

    //计算输入手续费
    var fee = parseFloat(val) * percentage;
    if (fee < min) {
      fee = min;
    } else if (fee > max) {
      fee = max;
    }
    fee = parseFloat(fee).toFixed(2);

    //根据余额计算手续费
    var b_fee = account_balance * percentage;//余额手续费
    if (b_fee < min) {
      b_fee = min;
    } else if (b_fee > max) {
      b_fee = max;
    }
    b_fee = parseFloat(b_fee.toFixed(2));
    var b_max = account_balance;//最大提现额度
    var is_all = false;
    if (b_max == parseFloat(val)) {
      is_all = true;
      fee = b_fee;
    }

    if (parseFloat(val) > b_max) {
      b_max = parseFloat(b_max).toFixed(2);
      // app.showToast('最大提现额度为：' + b_max);
      that.setData({
        actualMoney: '',
        allMoney: '',
        msg: '最大提现额度为：' + b_max,
        poundage: 0,
        money: 0,
        total: account_balance,
        isForward: false
      });
    } else {
      if (is_all) {
        var allMoney = b_max;
        var total = 0.00;
        val = account_balance;
      } else {
        var allMoney = val;
        var total = (account_balance - parseFloat(val)).toFixed(2);
      }
      var actualMoney = allMoney - fee;
      //转换格式
      fee = parseFloat(fee).toFixed(2);
      total = parseFloat(total).toFixed(2);
      actualMoney = parseFloat(actualMoney).toFixed(2);
      that.setData({
        allMoney: allMoney,
        total: total,
        money: val,
        poundage: fee,
        msg: '',
        actualMoney: actualMoney,
      });
    }
  },

  // 选择提现账户
  choosecard: function() {
    this.setData({
      prohibit: true
    })
  },

  // 关闭弹窗
  closemodel: function() {
    this.setData({
      prohibit: false
    })
  },

  // 选择银行卡
  cardItem: function(e) {
    var bank_id = e.currentTarget.dataset.id;
    this.setData({
      prohibit: false,
      isChoose: false,
      index: e.currentTarget.dataset.index,
      bank_id: bank_id
    });
  },

  //全部提现
  all(e) {
    var that = this;
    //计算手续费
    var available_money = parseFloat(that.data.workers_info.available_money);
    var percentage = parseFloat(withdrawConfig.value.percentage);
    var fee = available_money * percentage;
    fee = parseFloat(fee).toFixed(2);
    var min = parseFloat(withdrawConfig.value.min);
    var max = parseFloat(withdrawConfig.value.max);
    if (fee < min) {
      fee = min;
    } else if (fee > max) {
      fee = max;
    }
    
    var tmp = available_money;
    var min_money = parseFloat(withdrawConfig.value.min_money);
    if (tmp < min_money) {
      app.showToast('最低提现金额' + min_money);

      return false;
    }
    var max_money = parseFloat(withdrawConfig.value.max_money);

    if (tmp > max_money) {
      app.showToast('最高提现金额' + max_money);
      return false;
    }
    var actualMoney = tmp - fee;
    tmp = parseFloat(tmp).toFixed(2);
    var total = parseFloat(0).toFixed(2);
    actualMoney = parseFloat(actualMoney).toFixed(2);
    // 判断是否能提现
    var isForward = '';
    if (tmp < min_money || tmp > max_money) {
      isForward = false;
    } else {
      isForward = true;
    }
    that.setData({
      allMoney: tmp,
      total: total,
      poundage: fee,
      money: available_money,
      actualMoney: actualMoney,
      isForward: isForward
    })
  },

  // 提现
  extract() {
    var that = this;
    if(!that.data.isForward){
      return false;
    }
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if (!reg.test(that.data.money)) {
      app.showToast('请输入正确的提现金额');
      return false;
    }
    var money = Math.floor(parseFloat(that.data.money * 1000)) / 1000;
    var min_money = Math.floor(parseFloat(withdrawConfig.value.min_money * 1000)) / 1000;
    if (money < min_money) {
      app.showToast('最低提现金额' + withdrawConfig.value.min_money);
      return false;
    }
    var max_money = Math.floor(parseFloat(withdrawConfig.value.max_money * 1000)) / 1000;
    if (money > max_money) {
      app.showToast('最高提现金额' + withdrawConfig.value.max_money);
      return false;
    }
    if (isRepeat) {
      return;
    }
    isRepeat = true;
    app.ajax({
      url: "User/Channel/withdrawDepositApply",
      data: {
        user_id: wx.getStorageSync('user_id'),
        bank_id: that.data.bank_id,
        money: that.data.money
      },
      success: function(res) {
        isRepeat = false;
        if (res.data.code == 1000) {
          app.showToast(res.data.message)
          setTimeout(function() {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000);
        }else{
          app.showToast(res.data.message)
        }
      }
    })

  },

  // 获取提现信息
  withdrawDepositInfo() {
    var that = this;
    app.ajax({
      url: "User/Channel/withdrawDepositInfo",
      data: {
        user_id: wx.getStorageSync('user_id')
      }, 
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            banks: res.data.data.banks,
            total: res.data.data.account_info.available_money,
            workers_info: res.data.data.account_info
          })
        } else {
          app.showToast(res.data.message)
        }
      }
    })
  },
  // 获取提现配置
  withdrawConfig() {
    var that = this;
    app.ajax({
      url: "Common/Common/getBalanceConfig",
      data: {
        key: 'payment_fee'
      },
      success: function (res) {
        var result = res.data.data;
        if (result != null) {
            withdrawConfig= result;
        } else {
          app.showToast('提现功能已关闭')
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000);
        }
      }
    })
  },
})